package com.zeek.car90s.util;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;

import com.zeek.car90s.Constants;
import com.zeek.car90s.R;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/10.
 */
public class MediaPlayerUtils {
    private static MediaPlayer mediaPlayer;//播放长的背景音乐
    private static SoundPool sp;//播放短促的提示音
    public static MediaPlayerUtils mediaPlayerUtils;
    private static Map<Integer,Integer> soundIdMap;

    public static MediaPlayerUtils init(Context context) {

        sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
        if (mediaPlayerUtils == null) {
            mediaPlayerUtils = new MediaPlayerUtils();
        }

        if (mediaPlayer == null) {
            mediaPlayer = MediaPlayer.create(context, R.raw.bg0);//背景音乐
            mediaPlayer.setLooping(true);
        }

        if(soundIdMap==null){
            soundIdMap=new HashMap<>();
        }
        soundIdMap.put(1,sp.load(context, R.raw.click1, 1));//点击正常按钮时候的声音
//        soundIdMap.put(2,sp.load(context, R.raw.clickbtn, 1));//选择答案的时候的声音
        soundIdMap.put(2,sp.load(context, R.raw.correct, 1));//回答正确
        soundIdMap.put(3,sp.load(context, R.raw.wrong, 1));//回答错误

        return mediaPlayerUtils;
    }

    public static void play() {
        if (!mediaPlayer.isPlaying()&& Constants.isPlayMusic) {
            mediaPlayer.start();
        }
    }

    public static void pause() {
        if(mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
        }
    }

    //播放提示音,1-正常按钮，2-回答正确，3-回答错误
    public static void playNotification(int type){
        sp.play(soundIdMap.get(type), 1, 1, 0, 0, 1);
    }
}
